// var run=true;
//完成初始化
//待写
function init(){

    $.ajax({
        url:'/target/FindAllTarget',
        type:'get',
        dataType:'json',
        success:function (data){
            $("#lv-course-target").find('tr').remove();
            for(let i in data){
                let tr;

                 tr = $("<tr>");
                 let th1=$('<th scope="row">'+data[i].courseTargetId+'</th>' );
                 let th2=$('<td>' +data[i].courseTargetShortname+'</td>');

                 let th3=$('<td>' +data[i].courseTargetContent+'</td>');

                 tr.append(th1);
                 tr.append(th2);
                 tr.append(th3);
                btn_delete=$("<td> <input type=button value='删除' class='lv_button' /> </td>");
                btn_modify=$("<td> <input type=button value='修改' class='lv_button' /> </td>");
                tr.append(btn_delete);
                tr.append(btn_modify);

                $("#lv-course-target").append(tr);

                btn_delete.click(function (){


                    let tr_table=$(this).parent();
                     let td=tr_table.find("th").eq(0).html();
                    alert(td);
                    $.ajax({
                        url:'/target/DeleteTarget',
                        type:'post',
                        dataType: 'json',
                        // contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                        data:{"courseTargetId":td},

                        success:function (data){
                            console.log(data);
                            if(data)
                                alert("课程目标删除成功");
                            else
                            {

                                alert("删除课程目标失败");
                            }
                        }




                    })

                    $(this).parent().remove();





                })
                //


                btn_modify.click(function (){
                //   调用模态框
                //     alert("修改课程目标");
                    //显示修改框
                    // $(".lv_hide").removeClass("lv_hide");
                    //根据课程目标ID 返回一条数据
                    let t1=$(this).parent().find("th").eq(0).html();
                    $.ajax({
                        url:'/target/target',
                        type:'get',
                        dataType: 'json',
                        // contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                        data:{"courseTargetId":t1},

                        success:function (data){
                            console.log(data);
                            if(data)
                            {
                                //在模态框中显示想要修改的数据

                                $(".lv_hide").removeClass("lv_hide");
                                $("#add_courseTargetId").val(data.courseTargetId);
                                $("#add_courseId").val(data.courseId);


                                $("#add_courseTargetShortname").val(data.courseTargetShortname);
                                 let d=data.bulumuTarget;

                               let ch=$("input[name='add_bulumuTarget']:checked").val();
                               console.log("bulumu:   "+ch);
                                $("input[name='add_bulumuTarget']:checked").prop("checked",false);

                                switch (d){
                                    case 1:
                                        $("input[name='add_bulumuTarget'][value='1']").prop("checked",true);
                                        break;
                                    case 2:
                                        $("input[name='add_bulumuTarget'][value='2']").prop("checked",true);
                                        break;
                                    case 3:
                                        $("input[name='add_bulumuTarget'][value='3']").prop("checked",true);
                                        break;
                                    case 4:
                                        $("input[name='add_bulumuTarget'][value='4']").prop("checked",true);
                                        break;
                                    case 5:
                                        $("input[name='add_bulumuTarget'][value='5']").prop("checked",true);
                                        break;
                                    case 6:
                                        $("input[name='add_bulumuTarget'][value='6']").prop("checked",true);
                                        break;
                                    default:
                                        $("input[name='add_bulumuTarget'][value='6']").prop("checked",true);
                                        break;


                                }

                                console.log("shujuku bulumu  "+d);
                                console.log(4);

                                $("#add_courseTargetContent").val(data.courseTargetContent);
                                showkeywords();

                                alert("课程目标是"+data.courseTargetId);
                            }
                            else
                            {
                                alert("修改课程目标失败");
                            }
                        }




                    })


                    // $(this).parent().find("th").eq(0).html();






                //    显示内容


                //    提交修改









                })




                //之前的版本

                // $("#deleteCourseTarget_btn").click(function (){
                //
                //     $(this).remove();
                //     alert("失败2");
                //
                // })
                // tr='<th scope="row">'+data[i].courseTargetId+'</th>'
                //     +'<td>'+data[i].courseTargetShortname+'</td>'
                //     +'<td>'+data[i].courseTargetContent+'</td>'
                //     +'<td>'+'<input type=button value=删除 id="deleteCourseTarget_btn"/>'+'<input type=button value=修改 />'+'</td>'
                //
                //
                // $("#lv-course-target").append('<tr>'+tr+'</tr>');

            }
            // $(function (){
            //     //看不懂
            //     $("#lv-course-target tr").find("a").click(function(){
            //         let id = $(this).parents("tr").find('th').text();
            //         window.open('./target-input.html?courseId='+id,'_self');
            //     })
            // });
        }
    });
}
//完成课程目标的搜索
function searchTarget(){
    let shortname=$("#lv_search_target").val();
    console.log(shortname);
    $.ajax({
        url:'/target/SearchCourseTarget',
        type:'get',
        dataType:'json',
        data:{"courseTargetShortname":shortname},

        success:function (data){
            $("#lv-course-target").find('tr').remove();
            for(let i in data){
                let tr;

                //   第二种

                tr = $("<tr>");
                let th1=$('<th scope="row">'+data[i].courseTargetId+'</th>' );
                // th1.innerText=data[i].courseTargetId;
                let th2=$('<td>' +data[i].courseTargetShortname+'</td>');
                // th2.innerText=data[i].courseTargetShortname;
                let th3=$('<td>' +data[i].courseTargetContent+'</td>');
                // th3.innerText=data[i].courseTargetContent;
                tr.append(th1);
                tr.append(th2);
                tr.append(th3);
                btn_delete=$("<td> <input type=button value='删除' class='lv_button' /> </td>");
                btn_modify=$("<td> <input type=button value='修改' class='lv_button' /> </td>");
                tr.append(btn_delete);
                tr.append(btn_modify);

                $("#lv-course-target").append(tr);

                //删除课程目标

                btn_delete.click(function (){



                    // alert("删除成功2");
                    // let course_id=$(this).val();
                    // alert(course_id);

                    let tr_table=$(this).parent();
                    let td=tr_table.find("th").eq(0).html();
                    alert(td);
                    $.ajax({
                        url:'/target/DeleteTarget',
                        type:'post',
                        dataType: 'json',
                        // contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                        data:{"courseTargetId":td},

                        success:function (data){
                            console.log(data);
                            if(data)
                                alert("课程目标删除成功");
                            else
                            {

                                alert("删除课程目标失败");
                            }
                        }




                    })

                    $(this).parent().remove();





                })
                //


                btn_modify.click(function (){
                    //   调用模态框
                    //     alert("修改课程目标");
                    //显示修改框
                    // $(".lv_hide").removeClass("lv_hide");
                    //根据课程目标ID 返回一条数据
                    let t1=$(this).parent().find("th").eq(0).html();
                    $.ajax({
                        url:'/target/target',
                        type:'get',
                        dataType: 'json',
                        // contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                        data:{"courseTargetId":t1},

                        success:function (data){
                            console.log(data);
                            if(data)
                            {
                                //在模态框中显示想要修改的数据

                                $(".lv_hide").removeClass("lv_hide");
                                $("#add_courseTargetId").val(data.courseTargetId);
                                $("#add_courseId").val(data.courseId);


                                $("#add_courseTargetShortname").val(data.courseTargetShortname);
                                let d=data.bulumuTarget;

                                let ch=$("input[name='add_bulumuTarget']:checked").val();
                                console.log("bulumu:   "+ch);
                                $("input[name='add_bulumuTarget']:checked").prop("checked",false);

                                switch (d){
                                    case 1:
                                        $("input[name='add_bulumuTarget'][value='1']").prop("checked",true);
                                        break;
                                    case 2:
                                        $("input[name='add_bulumuTarget'][value='2']").prop("checked",true);
                                        break;
                                    case 3:
                                        $("input[name='add_bulumuTarget'][value='3']").prop("checked",true);
                                        break;
                                    case 4:
                                        $("input[name='add_bulumuTarget'][value='4']").prop("checked",true);
                                        break;
                                    case 5:
                                        $("input[name='add_bulumuTarget'][value='5']").prop("checked",true);
                                        break;
                                    case 6:
                                        $("input[name='add_bulumuTarget'][value='6']").prop("checked",true);
                                        break;
                                    default:
                                        $("input[name='add_bulumuTarget'][value='6']").prop("checked",true);
                                        break;


                                }

                                console.log("shujuku bulumu  "+d);
                                console.log(4);

                                $("#add_courseTargetContent").val(data.courseTargetContent);
                                showkeywords();

                                alert("课程目标是"+data.courseTargetId);
                            }
                            else
                            {
                                alert("修改课程目标失败");
                            }
                        }




                    })


                    // $(this).parent().find("th").eq(0).html();






                    //    显示内容


                    //    提交修改









                })




                //之前的版本

                // $("#deleteCourseTarget_btn").click(function (){
                //
                //     $(this).remove();
                //     alert("失败2");
                //
                // })
                // tr='<th scope="row">'+data[i].courseTargetId+'</th>'
                //     +'<td>'+data[i].courseTargetShortname+'</td>'
                //     +'<td>'+data[i].courseTargetContent+'</td>'
                //     +'<td>'+'<input type=button value=删除 id="deleteCourseTarget_btn"/>'+'<input type=button value=修改 />'+'</td>'
                //
                //
                // $("#lv-course-target").append('<tr>'+tr+'</tr>');

            }
            // $(function (){
            //     //看不懂
            //     $("#lv-course-target tr").find("a").click(function(){
            //         let id = $(this).parents("tr").find('th').text();
            //         window.open('./target-input.html?courseId='+id,'_self');
            //     })
            // });
        }
    });
}







$(document).ready(function(){



    //    完成初始化
    init();




    //显示关键词
    let showbloom=setInterval(showkeywords,1000);

    //  增加课程目标
    $("#AddCourse").click(function (){
        let CourseTargetId =$("#courseTargetId").val();
        let CourseId = $("#courseId").val();
        let CourseTargetContent=$("#courseTargetContent").val();
        let BulumuTarget=$("input[id='optionsRadios']:checked").val();
        let CourseTargetShortname=$("#courseTargetShortname").val();

        console.log(CourseTargetId,CourseId,CourseTargetShortname,CourseTargetContent,BulumuTarget);
        $.ajax({
            url:'/target/AddTarget',
            type:'post',
            dataType: 'json',
            // contentType:'application/x-www-form-urlencoded; charset=UTF-8',
            data:{"courseTargetId":CourseTargetId,"courseId":CourseId,
                "courseTargetContent":CourseTargetContent,"bulumuTarget":BulumuTarget,"courseTargetShortname":CourseTargetShortname},

            success:function (data){
                console.log(data);
                if(data)
                {
                    alert("课程目标增加成功");
                    //数据库无法完成更新就自己写一行
                    init();
                    alert("成功");
                }
                else
                {

                   alert("增加课程目标失败");
                }
            }




        })


    //    标记课程是否具有课程目标
        $.ajax({
            url:'/lv_course/updateHaveTarget',
            type:'post',
            dataType: 'json',
            // contentType:'application/x-www-form-urlencoded; charset=UTF-8',
            data:{"courseId":CourseId},

            success:function (data){
                alert("开始标记")
                console.log(data);
                if(data)
                {
                    alert("课程标记成功");
                    //数据库无法完成更新就自己写一行
                }
                else
                {

                    alert("增加课程目标失败");
                }
            }




        })

    })
//    取消修改
    $("#cancel_AddCourse").click(function (){

        $(".lv_modal").addClass("lv_hide");
        $(".lv_shade").addClass("lv_hide");

    })

//    提交修改
    $("#modify_AddCourse").click(function () {
        let CourseTargetId =$("#add_courseTargetId").val();
        let CourseId = $("#add_courseId").val();
        let CourseTargetContent=$("#add_courseTargetContent").val();
        let BulumuTarget=$("input[id='optionsRadios']:checked").val();
        let CourseTargetShortname=$("#add_courseTargetShortname").val();

        console.log(CourseTargetId,CourseId,CourseTargetShortname,CourseTargetContent,BulumuTarget);
        $.ajax({
            url:'/target/UpdateTarget',
            type:'post',
            dataType: 'json',
            // contentType:'application/x-www-form-urlencoded; charset=UTF-8',
            data:{"courseTargetId":CourseTargetId,"courseId":CourseId,
                "courseTargetContent":CourseTargetContent,"bulumuTarget":BulumuTarget,"courseTargetShortname":CourseTargetShortname},

            success:function (data){
                console.log(data);
                if(1)
                {
                    $(".lv_modal").addClass("lv_hide");
                    $(".lv_shade").addClass("lv_hide");
                    alert("课程目标修改成功");
                    //刷新
                    init();
                }
                else
                {

                    alert("课程目标修改失败");
                }
            }




        })


    })


//    完成课程目标的查询
    $("#lv_btn_findtarget").click(function (){
        searchTarget();
    })
//    刷新数据
    $("#lv_showAlltargets").click(function (){
        init();
    })





})


function showkeywords(){

    let BulumuTarget=$("input[id='optionsRadios']:checked").val();

    $.ajax({
        url:'/lv_Bloom/FindBloomTargetById',
        type:'get',
        dataType: 'json',
        // contentType:'application/x-www-form-urlencoded; charset=UTF-8',
        data:{"bulumuTarget":BulumuTarget},

        success:function (data){
            // console.log(data);
            if(data)
            {
                $("#bloom_tip").val(data.bulumuContent);


            }
            else
            {

                alert("关键词提示失败");
            }
        }




    })


//    刷新修改界面的关键词
    let BulumuTarget_1=$("input[id='add_bulumuTarget']:checked").val();
    $.ajax({
        url:'/lv_Bloom/FindBloomTargetById',
        type:'get',
        dataType: 'json',
        // contentType:'application/x-www-form-urlencoded; charset=UTF-8',
        data:{"bulumuTarget":BulumuTarget_1},

        success:function (data){
            console.log(data);
            if(data)
            {
                $("#bloom_tip_1").val(data.bulumuContent);


            }
            else
            {

                alert("课程目标修改失败");
            }
        }




    })





}
